//
//  NSString+DateFormatter.m
//  xinLing
//
//  Created by 于鹏 on 2017/7/19.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "NSString+DateFormatter.h"

@implementation NSString (DateFormatter)

+ (long)currentTimeInterval {
    NSDate *date = [NSDate date];
    long currentTimeInterval = [date timeIntervalSince1970];
    return currentTimeInterval;
}

+ (NSString *)standardTimeSince1970:(NSTimeInterval)time {
    NSDateFormatter *stampFormatter = [[NSDateFormatter alloc]init];
    [stampFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *stampDate = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *dateString = [stampFormatter stringFromDate:stampDate];
//    NSLog(@"dateString:%@",dateString);
    return dateString;
}

+ (NSString *)timeBeforeInfoWithTimeIntrval:(NSTimeInterval)timeIntrval{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    //获取此时时间戳长度
    NSTimeInterval nowTimeinterval = [[NSDate date] timeIntervalSince1970];
    int timeInt = nowTimeinterval - timeIntrval; //时间差
    
    int year = timeInt / (3600 * 24 * 30 *12);
    int month = timeInt / (3600 * 24 * 30);
    int day = timeInt / (3600 * 24);
    int hour = timeInt / 3600;
    int minute = timeInt / 60;
    int second = timeInt;
    if (year > 0) {
        return [NSString stringWithFormat:@"%d年以前",year];
    }else if(month > 0){
        return [NSString stringWithFormat:@"%d个月以前",month];
    }else if(day > 0){
        return [NSString stringWithFormat:@"%d天以前",day];
    }else if(hour > 0){
        return [NSString stringWithFormat:@"%d小时以前",hour];
    }else if(minute > 0){
        return [NSString stringWithFormat:@"%d分钟以前",minute];
    }else{
//        return [NSString stringWithFormat:@"刚刚"];
        return [NSString stringWithFormat:@"%d秒以前",second];

    }
}

+ (BOOL)isBeyondMinutes:(NSInteger)minutes withTime:(NSTimeInterval)timeInterval {
    //获取此时时间戳长度
    //    NSTimeInterval nowTimeInterval = [[NSDate date]timeIntervalSince1970];
    long nowTimeInterval = [self currentTimeInterval];
    int timeInt = nowTimeInterval - timeInterval;//时间差
    int minute = timeInt / 60;  //分钟
    if (minute > minutes) {
        return YES;
    } else {
        return NO;
    }
}
@end
